package cn.edu.scau.cmi.ema.dao;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.domain.Law;
import cn.edu.scau.cmi.ema.util.CmiSqlUtil;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.Set;

@Repository("LawDAO")
@Transactional
public class LawDAOImpl extends AbstractJpaDao<Law> implements LawDAO {

	private final static Set<Class<?>> dataTypes = new HashSet<Class<?>>(Arrays.asList(new Class<?>[] { Law.class }));

	@PersistenceContext()
	private EntityManager entityManager;

	public LawDAOImpl() {
		super();
	}

	public EntityManager getEntityManager() {
		return entityManager;
	}

	public Set<Class<?>> getTypes() {
		return dataTypes;
	}

	@Transactional
	public Law findLawById(Integer id) throws DataAccessException {

		return findLawById(id, -1, -1);
	}

	@Transactional
	public Law findLawById(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findLawById", startResult, maxRows, id);
			return (Law) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Law> findLawByName(String name) throws DataAccessException {

		return findLawByName(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Law> findLawByName(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findLawByName", startResult, maxRows, name);
		return new LinkedHashSet<Law>(query.getResultList());
	}

	@Transactional
	public Set<Law> findLawByNameContaining(String name) throws DataAccessException {

		return findLawByNameContaining(name, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Law> findLawByNameContaining(String name, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findLawByNameContaining", startResult, maxRows, name);
		return new LinkedHashSet<Law>(query.getResultList());
	}

	@Transactional
	public Set<Law> findLawByDescription(String description) throws DataAccessException {

		return findLawByDescription(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Law> findLawByDescription(String description, int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findLawByDescription", startResult, maxRows, description);
		return new LinkedHashSet<Law>(query.getResultList());
	}

	@Transactional
	public Set<Law> findLawByDescriptionContaining(String description) throws DataAccessException {

		return findLawByDescriptionContaining(description, -1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Law> findLawByDescriptionContaining(String description, int startResult, int maxRows)
			throws DataAccessException {
		Query query = createNamedQuery("findLawByDescriptionContaining", startResult, maxRows, description);
		return new LinkedHashSet<Law>(query.getResultList());
	}

	@Transactional
	public Law findLawByPrimaryKey(Integer id) throws DataAccessException {
		return findLawByPrimaryKey(id, -1, -1);
	}

	@Transactional
	public Law findLawByPrimaryKey(Integer id, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findLawByPrimaryKey", startResult, maxRows, id);
			return (Law) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Law> findLawsByIds(Set<Integer> ids) throws DataAccessException {
		try {
			Query query = createNamedQuery("findLawsByIds", -1, -1, ids);
			return new HashSet(query.getResultList());
		} catch (NoResultException nre) {
			return null;
		}
	}

	@Transactional
	public Set<Law> findAllLaws() throws DataAccessException {

		return findAllLaws(-1, -1);
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Law> findAllLaws(int startResult, int maxRows) throws DataAccessException {
		Query query = createNamedQuery("findAllLaws", startResult, maxRows);
		return new LinkedHashSet<Law>(query.getResultList());
	}

	@SuppressWarnings("unchecked")
	@Transactional
	public Set<Law> findLawByCompositeProperities(String sql, int startResult, int maxRows) {
		return new LinkedHashSet<Law>(createQuery(sql, startResult, maxRows).getResultList());
	}

	@Transactional
	public Set<Law> findLawsByDetachedCriteria(CmiPagination page) throws DataAccessException {
		DetachedCriteria detachedCriteria = CmiSqlUtil.createDetachedCriteria(page, Law.class);
		Session session = (Session) entityManager.getDelegate();
		Set<Law> laws = new HashSet(detachedCriteria.getExecutableCriteria(session)
				.setFirstResult(page.getSearchBeginCursor()).setMaxResults(page.getPaginationSize()).list());

		return laws;
	}

	@Transactional
	public Law findLawByPhysicalPrimaryKey(String name) throws DataAccessException {
		return findLawByPhysicalPrimaryKey(name, -1, -1);
	}

	@Transactional
	public Law findLawByPhysicalPrimaryKey(String name, int startResult, int maxRows) throws DataAccessException {
		try {
			Query query = createNamedQuery("findLawByPhyscialPrimaryKey", startResult, maxRows, name);
			return (Law) query.getSingleResult();
		} catch (NoResultException nre) {
			return null;
		}
	}

	public boolean canBeMerged(Law entity) {
		return true;
	}
}
